module.service('Item', ['$rootScope', function ($rootScope) {
    var service = {
        _baseUrl: "/azeroth/back/item/",
        currentItem: {},
        itemList: [],
        totalSceneCount: 0,
        queryList: function (currentPage, pageSize, title, userId, state, brandName, callback) {
            var paramData = {
                title: title,
                pageNum: currentPage,
                start: (currentPage - 1) * pageSize,
                pageSize: pageSize,
                brandName: brandName,
                needCount: true
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "getItemList.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.itemList);
                //对价格进行再次解析，除以100
                $.each(service.itemList, function (index, result) {
                    result['price'] = result['price'] / 100;
                    result['vipPrice'] = result['vipPrice'] / 100;
                    result['commission'] = result['commission'] / 100;
                    result['cashBack'] = result['cashBack'] / 100;
                    result['postage'] = result['postage'] / 100;
                });
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });
        },
        downItem: function (id, callback) {
            var paramData = {
                id: id
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "downItem.htm", paramData, function (resultJson) {
                //根据id删除掉该数据
                var removeIndex;
                $.each(service.itemList, function (index, result) {
                    if (result['id'] == id) {
                        removeIndex = index;
                    }
                });
                service.itemList.splice(removeIndex, 1);
                if (callback) {
                    callback();
                }
            });
        },
        queryDetail: function (id, callback) {
            var paramData = {
                itemId: id,
                needSku: true
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "getItemDetail.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.currentItem);
                service.currentItem['price'] = service.currentItem['price'] / 100;
                service.currentItem['vipPrice'] = service.currentItem['vipPrice'] / 100;
                service.currentItem['commission'] = service.currentItem['commission'] / 100;
                service.currentItem['cashBack'] = service.currentItem['cashBack'] / 100;
                service.currentItem['postage'] = service.currentItem['postage'] / 100;
                if (callback) {
                    callback();
                }
            });
        },
        insert: function (item, miniSkuList, callback, failCall) {
            var insertItem = item;
            delete insertItem.descPicList;
            //price 和 vipPrice 乘以 100
            insertItem.price = insertItem.price * 100;
            insertItem.vipPrice = insertItem.vipPrice * 100;
            insertItem.commission = insertItem.commission * 100;
            insertItem.cashBack = insertItem.cashBack * 100;

            var paramData = insertItem;
            paramData['miniSkuList'] = JSON.stringify(miniSkuList);

            $rootScope.ajaxRequestPost(service._baseUrl + "publishItem.htm", paramData, function (resultJson) {
                if (callback) {
                    callback();
                }
            }, failCall);
        },
        update: function (item, miniSkuList, callback) {
            var updateItem = item;
            delete updateItem.descPicList;
            //price 和 vipPrice 乘以 100
            updateItem.price = updateItem.price * 100;
            updateItem.vipPrice = updateItem.vipPrice * 100;
            updateItem.commission = updateItem.commission * 100;
            updateItem.cashBack = updateItem.cashBack * 100;
            updateItem.postage = updateItem.postage * 100;

            updateItem.itemSkuDOList = [];
            updateItem.featureValueDOList = [];
            updateItem.featureIdList = [];
            updateItem['miniSkuList'] = JSON.stringify(miniSkuList);
            $rootScope.ajaxRequestPost(service._baseUrl + "updateItem.htm", updateItem, function (resultJson) {
                if (callback) {
                    callback();
                }
            });
        }
    };
    return service;
}]);